import java.util.*;
import java.io.*;

public class JavaInvokeScript {
    public static void main(String[] args) throws IOException{
        ProcessBuilder pb = new ProcessBuilder("./test.sh","myArg1","myArg2");
        Map<String,String> env =  pb.environment();
        env.put("VAR1","myValue");
        env.put("VAR2", env.get("VAR1") + "suffix");
        pb.directory(new File("."));
        //pb.redirectOutput(new File("./test.txt"));
        Process p = pb.start();
        InputStream stream =  p.getInputStream();

        byte[] buffer = new byte[stream.available()];
        stream.read(buffer);

        File targetFile = new File("./test.txt");

        OutputStream outStream = new FileOutputStream(targetFile);
        outStream.write(buffer);
    }
}